package dshumsky.jooq;

import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Result;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import dshumsky.jooq.generated.Tables;
import dshumsky.jooq.generated.tables.GptCurrency;

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class CurrencyRepository implements InitializingBean {

	@Autowired
	DSLContext dsl;

	public void test() {
		System.out.println("111");
		Result<Record> result = dsl.select().from(Tables.GPT_CURRENCY).fetch();

		for (Record r : result) {
			Long id = r.getValue(GptCurrency.GPT_CURRENCY.ID);
			String code = r.getValue(GptCurrency.GPT_CURRENCY.CODE);

			System.out.println("id: " + id + " code: " + code);
		}
	}

	@Transactional(rollbackFor = Exception.class, readOnly = false)
	public void test2() {
		int execute = dsl.update(Tables.GPT_CURRENCY)
				.set(GptCurrency.GPT_CURRENCY.STATUS, (byte) 1)
				.where(GptCurrency.GPT_CURRENCY.ID.equal(380299524l)).execute();

		System.out.println("execute=" + execute);
		throw new RuntimeException();
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		// test();
	}
}
